package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.exception.LeadNewsException;
import com.itheima.common.util.RequestContextUtil;
import com.itheima.common.vo.PageResultVo;
import com.itheima.wemedia.dto.WmMaterialPageReqDto;
import com.itheima.wemedia.mapper.WmMaterialMapper;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmNewsMaterial;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmNewsMaterialService;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @description <p>自媒体图文素材信息 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.service.impl
 */
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Value("${fastdfs_server}")
    private String fastDfsServer;



    @Autowired
    private WmNewsMaterialService wmNewsMaterialService;
    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo pageList(WmMaterialPageReqDto dto) {
        //获取分页参数，构建分页构建器
        IPage<WmMaterial> pageInfo = new Page<>(dto.getPage(), dto.getSize());

        //定义条件构造器
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        //只准查询自己的素材--避免错误操作别人的素材
        lqw.eq(WmMaterial::getUserId, RequestContextUtil.getUserId());

        lqw.eq(null != dto.getIsCollection() && dto.getIsCollection().intValue() != 0,
                WmMaterial::getIsCollection,dto.getIsCollection());
        //查询
        page(pageInfo,lqw);
        //查询结果需要设置完整路径
        List<WmMaterial> wmMaterialList = pageInfo.getRecords();
        //判断集合是否为空
        if(!CollectionUtils.isEmpty(wmMaterialList)){
            wmMaterialList.forEach(m->{
                m.setUrl(fastDfsServer + m.getUrl());
            });
        }

        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),
                pageInfo.getTotal(),wmMaterialList);
    }

    /**
     * 收藏
     *
     * @param id
     */
    @Override
    public void collect(Long id) {
        //通过前端传过来的id查询素材表--为防止操作别人的素材条件加上userId
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        lqw.eq(null != id,WmMaterial::getId,id);
        lqw.eq(WmMaterial::getUserId,RequestContextUtil.getUserId());
        WmMaterial wmMaterial = getOne(lqw);
        wmMaterial.setIsCollection(1);
        update(wmMaterial,lqw);
    }

    /**
     * 取消收藏
     *
     * @param id
     */
    @Override
    public void cancelCollect(Long id) {
        //通过前端传过来的id查询素材表--为防止操作别人的素材条件加上userId
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        lqw.eq(null != id,WmMaterial::getId,id);
        lqw.eq(WmMaterial::getUserId,RequestContextUtil.getUserId());
        WmMaterial wmMaterial = getOne(lqw);
        wmMaterial.setIsCollection(0);
        update(wmMaterial,lqw);
    }

    /**
     * 删除素材
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean delete(Long id,Long userId) {
        //通过素材id和用户id查找素材
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        lqw.eq(null != id,WmMaterial::getId,id);

        lqw.eq(WmMaterial::getUserId,userId);

        //通过素材的id查询该素材是否被引用
        WmNewsMaterial wmNewsMaterial = wmNewsMaterialService.query().eq("material_id", id).one();
        if(!StringUtils.isEmpty(wmNewsMaterial)){
            throw new LeadNewsException("该素材已经被引用，禁止删除！");
        }
        if(!remove(lqw)){
            throw new LeadNewsException("删除失败");
        }
        return true;
    }
}
